package com.enterprise.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.assg2.dao.*;
import com.assg2.dao.DaoBook;
import com.movie.Book;
import javax.servlet.http.HttpSession;

/**
 * This is the command that will be used for logging in users.
 * If logon is successful, the command should place a list of contacts
 * in the request attriubutes.
 * @author  yunki
 */
public class FinishConfirmCommand implements Command {

    /** Creates a new instance of LoginCommand */
    public FinishConfirmCommand() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream

        //response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer

        HttpSession session = request.getSession(true);

        DaoOwner dow = new DaoOwner();
        DaoBook db = new DaoBook();
        if (request.getParameterValues("confirmed") != null) {
            String confirmed[] = request.getParameterValues("confirmed");
            for (int i = 0; i < confirmed.length; i++) {
                int bookid = Integer.parseInt(confirmed[i]);
                Book book = (Book)db.findById(bookid);
                book.setConfirm(Boolean.TRUE);
                db.update(book);
                
            }
            request.setAttribute("message", "Confirmation finished:<a href='/index.jsp'>Back to homepage</a>");
        return "message";
        }

        else{


        request.setAttribute("message", "Confirmation Error:<a href='javascript:history.go(-1)'>Go to Back</a>");
        return "message";
    }
    }
    //return "/footer.html").include(request, response);
    //return null;
}
